<template>
	<view>
		<view class="">
			<u--form labelPosition="left" :model="form" :rules="rules" ref="uForm" labelWidth='40' labelAlign="right"
				:labelStyle="labelStyle" errorType="message">
				<view class="bgw wrapper">
					<u-form-item label="名称" prop="mname" borderBottom required>
						<u--input v-model="form.mname" border="none" placeholder="请填写食材名称" v-if="is_edit_material"></u--input>
						<view class="" v-else>
							{{form.mname}}
						</view>
					</u-form-item>
					<view class="flex align-items-baseline">
						<view class="flex-1">
							<u-form-item label="形态" prop="form" borderBottom required>
								<view class="" v-show="false">
									<u--input v-model="form.form"></u--input>
								</view>
								<view class="flex-1" v-if="picker_config_obj.form.length>0">
									<picker @change="bindPicker" :value="picker_value_obj.form" :range="picker_config_obj.form"
										range-key="label" data-name="form" v-if="is_edit_material">
										<view class="flex">
											<view class="text-light-gray input-text flex-1" v-if="!form.form">
												请选择
											</view>
											<view class="input-text flex-1" v-else>
												{{picker_config_obj.form[picker_value_obj.form].label}}
											</view>
											<u-icon name="arrow-right"></u-icon>
										</view>
									</picker>
									<view class="input-text flex-1" v-else>
										{{picker_config_obj.form[picker_value_obj.form].label}}
									</view>
								</view>
							</u-form-item>
						</view>
						<view class="ml15 mr15"></view>
						<view class="flex-1">
							<u-form-item label="类别" prop="phase" borderBottom required>
								<view class="" v-show="false">
									<u--input v-model="form.phase"></u--input>
								</view>
								<view class="flex-1" v-if="picker_config_obj.phase.length>0">
									<picker @change="bindPicker" :value="picker_value_obj.phase" :range="picker_config_obj.phase"
										range-key="label" data-name="phase" v-if="is_edit_material">
										<view class="flex">
											<view class="text-light-gray input-text flex-1" v-if="!form.phase">
												请选择
											</view>
											<view class="input-text flex-1" v-else>
												{{picker_config_obj.phase[picker_value_obj.phase].label}}
											</view>
											<u-icon name="arrow-right"></u-icon>
										</view>
									</picker>
									<view class="input-text flex-1" v-else>
										{{picker_config_obj.phase[picker_value_obj.phase].label}}
									</view>
								</view>
							</u-form-item>
						</view>
					</view>
					<view class="flex align-items-baseline">
						<view class="flex-1">
							<u-form-item label="分类" prop="top_category_id" borderBottom required>
								<view class="" v-show="false">
									<u--input v-model="form.top_category_id"></u--input>
								</view>
								<view class="flex-1" v-if="picker_config_obj.top_category_id.length>0">
									<picker @change="bindPicker" :value="picker_value_obj.top_category_id"
										:range="picker_config_obj.top_category_id" range-key="label" data-name="top_category_id"
										v-if="is_edit_material">
										<view class="flex">
											<view class="text-light-gray input-text flex-1" v-if="!form.top_category_id">
												请选择
											</view>
											<view class="input-text flex-1" v-else>
												{{picker_config_obj.top_category_id[picker_value_obj.top_category_id].label}}
											</view>
											<u-icon name="arrow-right"></u-icon>
										</view>
									</picker>
									<view class="input-text flex-1" v-else>
										{{picker_config_obj.top_category_id[picker_value_obj.top_category_id].label}}
									</view>
								</view>
							</u-form-item>
						</view>
						<view class="ml15 mr15"></view>
					</view>
				</view>
				<u-gap height="20"></u-gap>
				<view class="wrapper font-bold">
					价格单位
				</view>
				<u-gap height="10"></u-gap>
				<view class="wrapper bgw">
					<view class="flex align-items-baseline">
						<view class="flex-1">
							<u-form-item label="主单位" required prop="main_unit_id" borderBottom labelWidth='55'>
								<view class="" v-show="false">
									<u--input v-model="form.main_unit_id"></u--input>
								</view>

								<view class="flex-1" v-if="picker_config_obj.main_unit_id.length>0">
									<picker @change="bindPicker" :value="picker_value_obj.main_unit_id"
										:range="picker_config_obj.main_unit_id" range-key="label" data-name="main_unit_id"
										v-if="is_edit_material">
										<view class="flex">
											<view class="text-light-gray input-text flex-1" v-if="!form.main_unit_id">
												请选择
											</view>
											<view class="input-text flex-1" v-else>
												{{picker_config_obj.main_unit_id[picker_value_obj.main_unit_id].label}}
											</view>
											<u-icon name="arrow-right"></u-icon>
										</view>
									</picker>
									<view class="input-text flex-1" v-else>
										{{picker_config_obj.main_unit_id[picker_value_obj.main_unit_id].label}}
									</view>
								</view>
							</u-form-item>
						</view>
						<view class="ml15 mr15"></view>
						<view class="flex-1">
							<u-form-item label="采购单位" prop="purchase_unit_id" borderBottom labelWidth='70' required>
								<view class="" v-show="false">
									<u--input v-model="form.purchase_unit_id"></u--input>
								</view>

								<view class="flex-1" v-if="picker_config_obj.purchase_unit_id.length>0">
									<picker @change="bindPicker" :value="picker_value_obj.purchase_unit_id"
										:range="picker_config_obj.purchase_unit_id" range-key="label" data-name="purchase_unit_id"
										v-if="is_edit_material">
										<view class="flex">
											<view class="text-light-gray input-text flex-1" v-if="!form.purchase_unit_id">
												请选择
											</view>
											<view class="input-text flex-1" v-else>
												{{picker_config_obj.purchase_unit_id[picker_value_obj.purchase_unit_id].label}}
											</view>
											<u-icon name="arrow-right"></u-icon>
										</view>
									</picker>
									<view class="input-text flex-1" v-else>
										{{picker_config_obj.purchase_unit_id[picker_value_obj.purchase_unit_id].label}}
									</view>
								</view>
							</u-form-item>
						</view>
					</view>
					<u-form-item label="主单位价格" required borderBottom labelWidth='85' prop="main_price">
						<u--input v-model="form.main_price" border="none" placeholder="请填写" type="number"
							v-if="is_edit_material"></u--input>
						<view class="" v-else>
							{{form.main_price}}
						</view>
					</u-form-item>
				</view>
				<u-gap height="20"></u-gap>

				<bomUnitRatio :main="form.main_unit_id" :purchase="form.purchase_unit_id"
					:units="picker_config_obj.main_unit_id" v-if="bom_unit_ratio" ref="bomUnitRatio"
					:init="bom_unit_ratio" :readonly="!is_edit_material">
				</bomUnitRatio>
				<view class="wrapper font-bold">
					采购
				</view>
				<u-gap height="10"></u-gap>
				<view class="wrapper bgw">
					<view class="flex align-items-baseline">
						<view class="flex-1">
							<u-form-item label="使用频次" prop="sel_weight" borderBottom required labelWidth='70'>
								<view class="" v-show="false">
									<u--input v-model="form.sel_weight"></u--input>
								</view>

								<view class="flex-1" v-if="picker_config_obj.sel_weight.length>0">
									<picker @change="bindPicker" :value="picker_value_obj.sel_weight"
										:range="picker_config_obj.sel_weight" range-key="label" data-name="sel_weight"
										v-if="is_edit_material">
										<view class="flex">
											<view class="text-light-gray input-text flex-1" v-if="!form.sel_weight">
												请选择
											</view>
											<view class="input-text flex-1" v-else>
												{{picker_config_obj.sel_weight[picker_value_obj.sel_weight].label}}
											</view>
											<u-icon name="arrow-right"></u-icon>
										</view>
									</picker>
									<view class="input-text flex-1" v-else>
										{{picker_config_obj.sel_weight[picker_value_obj.sel_weight].label}}
									</view>
								</view>
							</u-form-item>
						</view>

						<!-- <view class="flex-1">
							<u-form-item label="采购频次" prop="purchase_freq" borderBottom labelWidth='80' required>
								<u--input v-model="form.purchase_freq" v-show="false"></u--input>
								<view class="flex-1" v-if="picker_config_obj.purchase_freq.length>0">
									<picker @change="bindPicker" :value="picker_value_obj.purchase_freq"
										:range="picker_config_obj.purchase_freq" range-key="label" data-name="purchase_freq">
										<view class="flex">
											<view class="text-light-gray input-text flex-1" v-if="!form.purchase_freq">
												请选择
											</view>
											<view class="input-text flex-1" v-else>
												{{picker_config_obj.purchase_freq[picker_value_obj.purchase_freq].label}}
											</view>
											<u-icon name="arrow-right"></u-icon>
										</view>
									</picker>
								</view>
							</u-form-item>
						</view> -->
						<!-- <view class="ml15 mr15"></view> -->
						<!-- <view class="flex-1">
							<u-form-item label="采购分类" prop="purchase_category_id" borderBottom labelWidth='70' required>
								<view class="" v-show="false">
									<u--input v-model="form.purchase_category_id"></u--input>
								</view>

								<view class="flex-1" v-if="picker_config_obj.purchase_category_id.length>0">
									<picker @change="bindPicker" :value="picker_value_obj.purchase_category_id"
										:range="picker_config_obj.purchase_category_id" range-key="label" data-name="purchase_category_id"
										v-if="is_edit_material">
										<view class="flex">
											<view class="text-light-gray input-text flex-1" v-if="!form.purchase_category_id">
												请选择
											</view>
											<view class="input-text flex-1" v-else>
												{{picker_config_obj.purchase_category_id[picker_value_obj.purchase_category_id].label}}
											</view>
											<u-icon name="arrow-right"></u-icon>
										</view>
									</picker>
									<view class="input-text flex-1" v-else>
										{{picker_config_obj.purchase_category_id[picker_value_obj.purchase_category_id].label}}
									</view>
								</view>
							</u-form-item>
						</view> -->
					</view>
					<u-form-item label="首选供应商" prop="default_msupplier_id" labelWidth='85' required borderBottom>
						<view class="" v-show="false">
							<u--input v-model="form.default_msupplier_id"></u--input>
						</view>

						<view class="flex-1" v-if="msupplier.length>0">
							<view v-if="is_edit_material" @click="openMsupplierPicker">
								<view class="flex">
									<view class="text-light-gray input-text flex-1" v-if="!form.default_msupplier_id">
										请选择
									</view>
									<view class="input-text flex-1" v-else>
										{{msupplierObj[form.default_msupplier_id].supplier_nick_name}}
									</view>
									<u-icon name="arrow-right"></u-icon>
								</view>
							</view>
							<view class="input-text flex-1" v-else>
								{{msupplierObj[form.default_msupplier_id].supplier_nick_name}}
							</view>
						</view>
					</u-form-item>
					<view class="flex align-items-baseline">
						<view class="flex-1">
							<u-form-item label="采购起始值" required borderBottom labelWidth='85' prop="purchase_start_qty">
								<u--input v-model="form.purchase_start_qty" border="none" placeholder="请填写" type="number"
									v-if="is_edit_material"></u--input>
								<view class="" v-else>
									{{form.purchase_start_qty}}
								</view>
							</u-form-item>
						</view>
						<view class="ml15 mr15"></view>
						<view class="flex-1">
							<u-form-item label="增减幅度" required borderBottom labelWidth='70' prop="purchase_increment">
								<u--input v-model="form.purchase_increment" border="none" placeholder="请填写" type="number"
									v-if="is_edit_material"></u--input>
								<view class="" v-else>
									{{form.purchase_increment}}
								</view>
							</u-form-item>
						</view>

					</view>
				</view>
				<u-gap height="20"></u-gap>
				<view class="wrapper font-bold">
					仓管
				</view>
				<u-gap height="10"></u-gap>
				<view class="wrapper bgw">
					<view class="flex align-items-baseline">
						<view class="flex-1">
							<u-form-item label="入库方式" prop="stock_in_way" labelWidth='70' required borderBottom>
								<view class="" v-show="false">
									<u--input v-model="form.stock_in_way"></u--input>
								</view>

								<view class="flex-1" v-if="picker_config_obj.stock_in_way.length>0">
									<picker @change="bindPicker" :value="picker_value_obj.stock_in_way"
										:range="picker_config_obj.stock_in_way" range-key="label" data-name="stock_in_way"
										v-if="is_edit_material">
										<view class="flex">
											<view class="text-light-gray input-text flex-1" v-if="!form.stock_in_way">
												请选择
											</view>
											<view class="input-text flex-1" v-else>
												{{picker_config_obj.stock_in_way[picker_value_obj.stock_in_way].label}}
											</view>
											<u-icon name="arrow-right"></u-icon>
										</view>
									</picker>
									<view class="input-text flex-1" v-else>
										{{picker_config_obj.stock_in_way[picker_value_obj.stock_in_way].label}}
									</view>
								</view>
							</u-form-item>
						</view>
						<view class="ml15 mr15"></view>
						<view class="flex-1">
							<u-form-item label="出库类型" prop="in_out_way" labelWidth='70' required borderBottom>
								<view class="" v-show="false">
									<u--input v-model="form.in_out_way"></u--input>
								</view>

								<view class="flex-1" v-if="picker_config_obj.in_out_way.length>0">
									<picker @change="bindPicker" :value="picker_value_obj.in_out_way"
										:range="picker_config_obj.in_out_way" range-key="label" data-name="in_out_way"
										v-if="is_edit_material">
										<view class="flex">
											<view class="text-light-gray input-text flex-1" v-if="!form.in_out_way">
												请选择
											</view>
											<view class="input-text flex-1" v-else>
												{{picker_config_obj.in_out_way[picker_value_obj.in_out_way].label}}
											</view>
											<u-icon name="arrow-right"></u-icon>
										</view>
									</picker>
									<view class="input-text flex-1" v-else>
										{{picker_config_obj.in_out_way[picker_value_obj.in_out_way].label}}
									</view>
								</view>
							</u-form-item>
						</view>
					</view>
					<view class="flex align-items-baseline">
						<view class="flex-1">
							<u-form-item label="库位" prop="stock_top_loc_id" labelWidth='40' required borderBottom>
								<view class="" v-show="false">
									<u--input v-model="form.stock_top_loc_id"></u--input>
								</view>

								<view class="flex-1" v-if="picker_config_obj.stock_top_loc_id.length>0">
									<picker @change="bindPicker" :value="picker_value_obj.stock_top_loc_id"
										:range="picker_config_obj.stock_top_loc_id" range-key="label" data-name="stock_top_loc_id"
										v-if="is_edit_material">
										<view class="flex">
											<view class="text-light-gray input-text flex-1" v-if="!form.stock_top_loc_id">
												请选择
											</view>
											<view class="input-text flex-1" v-else>
												{{picker_config_obj.stock_top_loc_id[picker_value_obj.stock_top_loc_id].label}}
											</view>
											<u-icon name="arrow-right"></u-icon>
										</view>
									</picker>
									<view class="input-text flex-1" v-else>
										{{picker_config_obj.stock_top_loc_id[picker_value_obj.stock_top_loc_id].label}}
									</view>
								</view>
							</u-form-item>
						</view>
						<view class="ml15 mr15"></view>
						<view class="flex-1">
							<u-form-item label="出库成本" prop="daily_cost_category_id" borderBottom labelWidth='70' required>
								<view class="" v-show="false">
									<u--input v-model="form.daily_cost_category_id"></u--input>
								</view>

								<view class="flex-1" v-if="picker_config_obj.daily_cost_category_id.length>0">
									<picker @change="bindPicker" :value="picker_value_obj.daily_cost_category_id"
										:range="picker_config_obj.daily_cost_category_id" range-key="label"
										data-name="daily_cost_category_id" v-if="is_edit_material">
										<view class="flex">
											<view class="text-light-gray input-text flex-1" v-if="!form.daily_cost_category_id">
												请选择
											</view>
											<view class="input-text flex-1" v-else>
												{{picker_config_obj.daily_cost_category_id[picker_value_obj.daily_cost_category_id].label}}
											</view>
											<u-icon name="arrow-right"></u-icon>
										</view>
									</picker>
									<view class="input-text flex-1" v-else>
										{{picker_config_obj.daily_cost_category_id[picker_value_obj.daily_cost_category_id].label}}
									</view>
								</view>
							</u-form-item>
						</view>
					</view>
					<!-- <u-form-item label="提前" prop="mname"   labelWidth='50'  required>
						<view class="flex">
							<view class="text-light-gray input-text flex-1">
								请选择
							</view>
							<u-icon name="arrow-right"></u-icon>
						</view>
						<view class="" slot="right">
							天发料
						</view>
					</u-form-item> -->
				</view>
			</u--form>
		</view>
		<u-gap height="20"></u-gap>
		<view class="bottom wrapper" v-if="is_edit_material">
			<view class="flex">
				<view class="flex-1">
					<u-button text="重置" type="info" @click="reset"></u-button>
				</view>
				<view class="ml15 mr15"></view>
				<view class="flex-1">
					<u-button text="提交" type="primary" @click="submit"></u-button>
				</view>
			</view>
		</view>

		<msupplierPicker :show="msupplierPickerShow" :msupplier="msupplier" @confirm="msupplierPickerConfirm"
			@cancel="msupplierPickerCancel" v-if="msupplier.length>0"></msupplierPicker>
	</view>
</template>

<script>
	import bomUnitRatio from './components/bom-unit-ratio.vue';
	import msupplierPicker from '@/pagesA/components/msupplier-picker.vue';

	import {
		purchase_request_draft,
		material_create,
		material_write,
		material_create_edit,
		material_static_data,
		purchase_material_item
	} from '@/common/api.js';
	let today = new Date();
	let date = new Date(today.getTime() + (24 * 60 * 60 * 1000 * 1)); //明天
	let year_month_day = `${date.getFullYear()}` + `-` + `${date.getMonth()+1}`.padStart(2, '0') + `-` +
		`${date.getDate()}`.padStart(2, '0');
	let date_end = new Date(today.getTime() + (24 * 60 * 60 * 1000 * 7));
	let year_month_day_end = `${date_end.getFullYear()}` + `-` + `${date_end.getMonth()+1}`.padStart(2, '0') + `-` +
		`${date_end.getDate()}`.padStart(2, '0');
	export default {
		components: {
			bomUnitRatio,
			msupplierPicker
		},
		data() {
			return {
				fromPageName:'',
				is_edit_material: false,
				id: 0,
				is_quote: false,
				detail: null,
				labelStyle: {
					'font-weight': 'bold'
				},
				msupplier: [],
				msupplierObj: {},
				msupplierPickerShow: false,
				oldForm: {},
				old_picker_value_obj: {},
				form: {
					mname: '',
					form: '',
					phase: '',
					main_unit_id: '',
					purchase_unit_id: '',
					// main_unit_bom_unit_ratio: '',
					// default_process_id: '',
					main_price: '',
					top_category_id: '',
					// purchase_category_id: '',
					// plan_day_purchase_ahead_days: '',
					purchase_freq: '',
					daily_cost_category_id: '',
					default_msupplier_id: '',
					purchase_first_to: '',
					in_out_way: '',
					// out_days_ahead: '',
					// weight_ratio: '',
					// volume_ratio: '',
					sel_weight: '',
					stock_in_way: '',
					purchase_increment: 1,
					purchase_start_qty: 1,
					stock_top_loc_id: ''
				},
				picker_config_obj: {
					form: [],
					phase: [],
					main_unit_id: [],
					purchase_unit_id: [],
					top_category_id: [],
					// purchase_category_id: [],
					purchase_freq: [],
					daily_cost_category_id: [],
					// default_msupplier_id: [],
					purchase_first_to: [],
					in_out_way: [],
					sel_weight: [],
					stock_in_way: [],
					stock_top_loc_id: []
				},
				picker_value_obj: {
					form: 0,
					phase: 0,
					main_unit_id: 0,
					purchase_unit_id: 0,
					top_category_id: 0,
					// purchase_category_id: 0,
					purchase_freq: 0,
					daily_cost_category_id: 0,
					// default_msupplier_id: 0,
					purchase_first_to: 0,
					in_out_way: 0,
					sel_weight: 0,
					stock_in_way: 0,
					stock_top_loc_id: 0
				},
				rules: {
					'mname': [{
							type: 'string',
							required: true,
							message: '请填写食材名称',
							trigger: ['blur', 'change']
						},
						{
							validator: (rule, value, callback) => {
								const isChineseStr = (str) => {
									return /^[\u4E00-\u9FA5]+$/.test(str)
								}
								return isChineseStr(value);
							},
							trigger: ['blur', 'change'],
							message: '食材名称必须中文'
						}
					],
					'form': {
						type: 'string',
						required: true,
						message: '请选择形态类别',
						trigger: ['blur', 'change']
					},
					'phase': {
						type: 'string',
						required: true,
						message: '请选择食材类别',
						trigger: ['blur', 'change']
					},
					'top_category_id': {
						type: 'number',
						required: true,
						message: '请选择食材分类',
						trigger: ['blur', 'change']
					},
					'main_unit_id': {
						type: 'number',
						required: true,
						message: '请选择主单位',
						trigger: ['blur', 'change']
					},
					'purchase_unit_id': {
						type: 'number',
						required: true,
						message: '请选择采购单位',
						trigger: ['blur', 'change']
					},
					'main_price': {
						type: 'string',
						required: true,
						message: '请填写主单位价格',
						trigger: ['blur', 'change']
					},
					'purchase_freq': {
						type: 'string',
						required: true,
						message: '请选择采购频次',
						trigger: ['blur', 'change']
					},
					// 'purchase_category_id': {
					// 	type: 'number',
					// 	required: true,
					// 	message: '请选择采购分类',
					// 	trigger: ['blur', 'change']
					// },
					'daily_cost_category_id': {
						type: 'number',
						required: true,
						message: '请选择成本类别',
						trigger: ['blur', 'change']
					},
					'default_msupplier_id': {
						type: 'number',
						required: true,
						message: '请选择首选供应商',
						trigger: ['blur', 'change']
					},
					'purchase_start_qty': [{
							type: 'string',
							required: true,
							message: '请输入',
							trigger: ['blur', 'change']
						},
						{
							validator: (rule, value, callback) => {
								if (Number(value) < 0 || Number(value) == 0) {
									return false
								} else {
									return true
								}
							},
							trigger: ['blur', 'change'],
							message: '不得小于0'
						}
					],
					'purchase_increment': [{
							type: 'string',
							required: true,
							message: '请输入',
							trigger: ['blur', 'change']
						},
						{
							validator: (rule, value, callback) => {
								if (Number(value) < 0 || Number(value) == 0) {
									return false
								} else {
									return true
								}
							},
							trigger: ['blur', 'change'],
							message: '不得小于0'
						}
					],
					'in_out_way': {
						type: 'string',
						required: true,
						message: '请选择出库类型',
						trigger: ['blur', 'change']
					},
					'stock_in_way': {
						type: 'string',
						required: true,
						message: '请选择入库方式',
						trigger: ['blur', 'change']
					},
					'stock_top_loc_id': {
						type: 'number',
						required: true,
						message: '请选择库位',
						trigger: ['blur', 'change']
					}
				},
				bom_unit_ratio_ids: [],
				need_day: year_month_day,
				bom_unit_ratio: false
			}
		},
		methods: {
			msupplierPickerCancel(e) {
				this.msupplierPickerShow = false;
			},
			msupplierPickerConfirm(e) {
				this.msupplierPickerShow = false;
				this.form.default_msupplier_id = e.id;
			},
			openMsupplierPicker() {
				this.msupplierPickerShow = true;
			},
			bindPicker(e) {
				let key = e.currentTarget.dataset.name;
				let index = e.detail.value;
				this.picker_value_obj[key] = index;
				this.form[key] = this.picker_config_obj[key][index].id;
			},
			reset() {
				this.form = JSON.parse(JSON.stringify(this.oldForm));
				this.picker_value_obj = JSON.parse(JSON.stringify(this.old_picker_value_obj));
				this.$refs.bomUnitRatio.reset()
			},
			submit() {
				let bomUnitRatioValidate = this.$refs.bomUnitRatio.check();
				this.$refs.uForm.validate().then(res => {
					if (!bomUnitRatioValidate) {
						uni.$u.toast('请完善信息')
					} else {
						// uni.$u.toast('校验通过')
						if (this.id) {
							this.material_write();
						} else {
							this.material_create();
						}
					}
				}).catch(errors => {
					uni.$u.toast('请完善信息')
				})
			},
			material_create() {
				let bom_unit_ratio_ids = this.$refs.bomUnitRatio.getList().list;
				let vals = JSON.parse(JSON.stringify(this.form));
				vals.main_price = Number(vals.main_price);
				vals.purchase_increment = Number(vals.purchase_increment);
				vals.purchase_start_qty = Number(vals.purchase_start_qty);
				vals.bom_unit_ratio_ids = bom_unit_ratio_ids;
				// console.log(vals);
				// debugger
				uni.showLoading({
					title: '加载中'
				});
				material_create({
					vals
				}).then((res) => {
					// this.get_purchase_material_item();
					this.get_dish_material_common_data();
					uni.$u.toast('成功');
					setTimeout(() => {
						if(this.fromPageName){
							uni.$emit('refresh_'+this.fromPageName);
						};
						uni.navigateBack();
					}, 2000);
				}).catch(() => {

				}).finally(() => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
				})
			},
			material_write() {
				let bom_list = this.$refs.bomUnitRatio.getList();
				let vals = JSON.parse(JSON.stringify(this.form));
				vals.unlink_ratio_list = bom_list.delIds;
				vals.create_ratio_list = bom_list.list.filter(v => !v.id);
				vals.write_ratio_list = bom_list.list.filter(v => v.id);
				vals.id = this.id;
				vals.main_price = Number(vals.main_price);
				vals.purchase_increment = Number(vals.purchase_increment);
				vals.purchase_start_qty = Number(vals.purchase_start_qty);
				// console.log(vals);
				// debugger
				uni.showLoading({
					title: '加载中'
				});
				material_write({
					vals
				}).then((res) => {
					uni.$u.toast('成功');
					// this.get_purchase_material_item();
					this.get_dish_material_common_data();
					setTimeout(() => {
						if(this.fromPageName){
							uni.$emit('refresh_'+this.fromPageName);
						};
						uni.navigateBack();
					}, 2000);
				}).catch(() => {

				}).finally(() => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
				})
			},
			setForm(material) {
				// console.log(material);
				// console.log(bom_unit_ratio);
				for (let key in this.form) {
					if (typeof material[key] == 'object') {
						this.form[key] = material[key][0];
					} else {
						if (key == 'main_price' || key == 'purchase_increment' || key == 'purchase_start_qty') {
							this.form[key] = material[key] + '';
						} else {
							this.form[key] = material[key] || '';
						}
					}
				}
				this.form.mm_id = material.mm_id ? material.mm_id[0] : 0;
				this.form.is_quote = this.is_quote;
			},
			getData() {
				uni.showLoading({
					title: '加载中'
				});
				let data = {};
				if (this.id) {
					data.material_id = this.id;
				}
				material_create_edit(data).then((res) => {
					let {
						// daily_cost_category,
						// material_top_category,
						// material_purchase_category,
						// material_purchase_unit_category,
						msupplier,
						specfic_material_unit_ratio
						// selection_fields,
						// material_item,
						// material_unit_ratio,
						// stock_top_loc_category
					} = res.result;

					this.msupplier = msupplier;
					this.msupplierObj = {};
					msupplier.forEach(v => {
						this.msupplierObj[v.id] = v;
					})
					
					this.bom_unit_ratio = specfic_material_unit_ratio ||[];
				}).catch(() => {

				}).finally(() => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
				})
			},
			get_purchase_material_item(){
				purchase_material_item().then((res) => {
					this.$store.commit('set_material_item', res.result);	
				}).catch(() => {
				
				}).finally(() => {
					
				})
			},
			init() {
				let material_purchase_unit_category = this.$store.getters.get_material_purchase_unit_category;
				let material_top_category = this.$store.getters.get_material_top_category;
				let daily_cost_category = this.$store.getters.get_daily_cost_category;
				let material_static_data = this.$store.getters.get_material_static_data;
				// console.log(material_static_data);
				let {
						// daily_cost_category,
						// material_top_category,
						// material_purchase_category,
						// material_purchase_unit_category,
						// msupplier,
						selection_fields,
						// material_unit_ratio,
						stock_top_loc_category
				} = material_static_data;
				for (let key in selection_fields) {
					if (typeof selection_fields[key] == 'object' && key != 'supply_type') {
						console.log(key);
						if (!this.detail) {
							this.form[key] = selection_fields[key].default;
						}
						selection_fields[key]['选项'].forEach(v => {
							!this.picker_config_obj[key] && (this.picker_config_obj[key] = []);
							this.picker_config_obj[key].push({
								label: v[1],
								id: v[0]
							})
						})
					}
					if (typeof selection_fields[key] != 'object' && key != 'supply_type') {
						if (!this.id) {
							if (key == 'main_price' || key == 'purchase_increment' || key == 'purchase_start_qty') {
								this.form[key] = selection_fields[key] + '';
							} else {
								this.form[key] = selection_fields[key];
							}
						}
					}
				}
				// 选项
				this.picker_config_obj.main_unit_id = [];
				this.picker_config_obj.purchase_unit_id = [];
				material_purchase_unit_category.forEach(v => {
					this.picker_config_obj.main_unit_id.push({
						label: v.name,
						id: v.id
					})
					this.picker_config_obj.purchase_unit_id.push({
						label: v.name,
						id: v.id
					})
				})
				// this.picker_config_obj.purchase_category_id = [];
				// material_purchase_category.forEach(v => {
				// 	this.picker_config_obj.purchase_category_id.push({
				// 		label: v.name,
				// 		id: v.id
				// 	})
				// })
				this.picker_config_obj.top_category_id = [];
				material_top_category.forEach(v => {
					this.picker_config_obj.top_category_id.push({
						label: v.name,
						id: v.id
					})
				})
				this.picker_config_obj.daily_cost_category_id = [];
				daily_cost_category.forEach(v => {
					this.picker_config_obj.daily_cost_category_id.push({
						label: v.name,
						id: v.id
					})
				})
				this.picker_config_obj.stock_top_loc_id = [];
				stock_top_loc_category.forEach(v => {
					this.picker_config_obj.stock_top_loc_id.push({
						label: v.name,
						id: v.id
					})
				});
				
				for (let key in this.picker_config_obj) {
					if (this.form[key]) {
						this.picker_value_obj[key] = this.picker_config_obj[key].findIndex(v => v.id == this.form[key]);
						if (this.picker_value_obj[key] == -1) {
							this.picker_value_obj[key] = 0;
						}
					} else {
						this.picker_value_obj[key] = 0;
						// this.form[key] = this.picker_config_obj[key][0].id;
					}
				}
				this.oldForm = JSON.parse(JSON.stringify(this.form));
				this.old_picker_value_obj = JSON.parse(JSON.stringify(this.picker_value_obj));
			}
		},
		onLoad(d) {
			// d.id = '1764';
			// d.is_quote = '1';
			this.id = Number(d.id) || 0;
			this.fromPageName = d.from || '';
			if (d.detail) {
				let detail = JSON.parse(d.detail);
				let material_obj = this.$store.getters.get_material_obj;
				
				let material = {};
				if(this.id){
					material = material_obj[this.id] || {};
				}
				console.log(material);
				this.detail = Object.assign({}, detail, material);;
				this.setForm(this.detail);
			}
			
			this.is_quote = d.is_quote == '1';
			this.init();
			this.getData();
			try {
				let role = this.$store.getters.get_role;
				// console.log(role);
				this.is_edit_material = role['采购模块'].is_edit_material;
			} catch (e) {

			}
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules);
			if (!this.is_edit_material) {
				uni.setNavigationBarTitle({
					title: `食材详情`,
				});
				return;
			}
			if (this.id) {
				uni.setNavigationBarTitle({
					title: `修改食材`,
				});
			} else {
				uni.setNavigationBarTitle({
					title: `创建食材`,
				});
			}

		},

	}
</script>

<style>
	.input-text {
		font-size: 15px;
	}

	.required-item {
		position: relative;
	}

	.required-item::before {
		content: "*";
		color: #E40A0C;
		position: absolute;
		font-weight: bold;
		top: 12px;
		left: 0;
	}

	.align-items-baseline {
		align-items: baseline;
	}

	.fixed-bottom {
		padding-top: 10px;
		padding-bottom: 10px;
		border-top: 1px solid #cccccc;
		z-index: 9999;
	}

	.bottom {
		padding-top: 10px;
		padding-bottom: 10px;
		/* border-top: 1px solid #cccccc; */
		/* z-index: 9999; */
	}
</style>